package com.qf.student.web;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.qf.student.entity.Dept;
import com.qf.student.service.IDeptService;
import com.qf.student.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @version1.0
 * @auth dmh
 */

@WebServlet(urlPatterns = "/Dept")
public class DeptController extends HttpServlet {

    private IDeptService iDeptService = new DeptServiceImpl();

    public DeptController(){

    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 1.判断获取action
        String action = req.getParameter("action");

        // 2.根据action来调用不同的方法
        if ("add".equals(action)) {
            // 执行添加操作
            addStudent(req,resp);
        }else if ("del".equals(action)) {
            del(req,resp);
        }  else if ("update".equals(action)) {
            update(req,resp);
        } else if ("byId".equals(action)) {
            byId(req,resp);
        } else if ("list".equals(action)) {
            list(req,resp);
        }else if("checkName".equals(action)){
            checkName(req,resp);
        }else if("all".equals(action)){//返回部门所有的JSON数据
            all(req,resp);
        }
    }

    private void all(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 1.调用业务层获取数据
        List<Dept> list = iDeptService.list();

//        req.setAttribute("deptList", list);
        // 2.把集合转成JSON字符串响应给前端
        String jsonString = JSON.toJSONString(list);

        // 3.响应
        resp.getWriter().write(jsonString);
    }

    private void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String dname = req.getParameter("dname");
        Dept dept = iDeptService.getByName(dname);
        if(dept!=null){
            resp.getWriter().write("["+dname+"] 已存在");
        }else{
            resp.getWriter().write("["+dname+"] 不存在,可以使用");
        }
    }

    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1.获取数据
        String deptno = req.getParameter("deptno");

        //类型转换
        int id2 = Integer.parseInt(deptno);
        //删除
        boolean remove = iDeptService.remove(id2);
        //判断
        if(remove){
            //删除成功回到查询列表
            resp.sendRedirect("/Dept?action=list");

        }else{
            resp.getWriter().write("删除失败!");
        }
    }

    private void list(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //1.查询数据
        List<Dept> list = iDeptService.list();

        //2.把数据放到作用域中
        req.setAttribute("deptList",list);


        //3.转发到视图转发
        req.getRequestDispatcher("deptList.jsp").forward(req,resp);

    }

    private void byId(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {

        //获取传递过来的id
        String id2 = req.getParameter("deptno");

        //根据id查询学生信息
        Dept dept = iDeptService.getById(Integer.parseInt(id2));

        //将学生对象方进域对象里面
        req.setAttribute("dept",dept);

        //跳转更新页面
        req.getRequestDispatcher("updateDept.jsp").forward(req,resp);

    }

    private void update(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        // 1、更新肯定会有参数的，所以需要获取参数
        String deptno = req.getParameter("deptno");
        String dname = req.getParameter("dname");
        String loc = req.getParameter("loc");


        // 2、封装到成一个对象
        Dept dept = new Dept();
        dept.setDeptno(Integer.parseInt(deptno));
        dept.setDname(dname);
        dept.setLoc(loc);
        // 3.调用业务层修改学生数据
        boolean update = iDeptService.update(dept);
        if(update){
            //更新成功就跳转页面到列表(用重定向)
            resp.sendRedirect("Dept?action=list");
        }else{
            resp.getWriter().write("更新失败");
        }

    }

    private Dept studentById(HttpServletRequest req) {
            String deptno = req.getParameter("deptno");
            return iDeptService.getById(Integer.parseInt(deptno));
        }


    private void addStudent(HttpServletRequest req,HttpServletResponse resp) throws IOException {

        // 1、添加肯定会有参数的，所以需要获取参数
        // 1、更新肯定会有参数的，所以需要获取参数
        String deptno = req.getParameter("deptno");
        String dname = req.getParameter("dname");
        String loc = req.getParameter("loc"); // 用户输入男和女



        // 2、封装到成一个对象
        Dept dept = new Dept();
        dept.setDeptno(Integer.parseInt(deptno));
        dept.setDname(dname);
        dept.setLoc(loc);
        // 3.调用业务层添加学生
        boolean save = iDeptService.save(dept);
        // 根据添加的结果响应用户
        if (save) {
            //如果添加成功了就展示页面给用户看,跳转到列表页面
//                resp.getWriter().write("添加成功");
            resp.sendRedirect("Dept?action=list");//重定向是两次请求
        } else {
            resp.getWriter().write("添加失败");
        }
    }
}